<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Miners\Miner;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;


class UpdateMinerPaytoday extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'miners:update-miners-paytoday';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update Miners table pay_today column, ';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $activeMiners = Miner::where('status','active')->get();
        foreach ($activeMiners as $activeMiner) {
            $today = Carbon::today()->toDateString();
            $minerPayouts = $activeMiner
                ->payouts()
                ->whereDate('made_at',$today)
                ->get(['amount']);
            $payTodayAmount = 0;
            foreach ($minerPayouts as $minerPayout) {
                //var_dump($minerPayout->amount);
                $payTodayAmount += $minerPayout->amount;
            }
            $activeMiner->pay_today = $payTodayAmount;
            $activeMiner->save();
        }

    }
}
